package com.my.springbootsecurityjwt.config.security;

import cn.hutool.json.JSONUtil;
import com.my.common.result.R;
import com.my.common.result.ServletUtils;
import jakarta.servlet.ServletException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 当访问接口没有权限时，自定义的返回结果
 *
 * @author: marshal
 * @date: 2021-10-15 3:24 PM
 */
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        ServletUtils.renderString(response, JSONUtil.toJsonStr(R.failed(accessDeniedException.getMessage())));
    }
}
